package com.algorithm.ch1.cjm2.array;
import com.sun.tools.javac.util.Assert;

/**
 * Created by jieming.chen on 2019-07-16
 */
public class RemoveDuplicates {

    public static void main(String[] args) {
        int[] nums = {0,0,1,1,1,2,2,3,3,4};
        int len = removeDuplicates(nums);
        for (int i = 0; i < len; i++) {
            System.out.println(nums[i]);
        }
        int[] nums1 = {1};
        Assert.check(removeDuplicates(nums1) == 1);
    }

    private static int removeDuplicates(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int i = 0, len = nums.length;
        while (i < len -1) {
            int j = i + 1;
            if (nums[i] < nums[j]) {
                i++;
            } else {
                while (j < nums.length -1) {
                    nums[j] = nums[j + 1];
                    j++;
                }
                len--;
            }
        }
        return len;
    }

    private static int removeDuplicates1(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int index = 0;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] != nums[index]) {
                nums[++index] = nums[i];
            }
        }
        return index + 1;
    }

}
